package com.xhwl.logistics.converter;

import cn.hutool.core.date.DateUtil;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.input.QueryVehiclePayRecordStatisticsBO;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.dto.query.VehiclePayRecordStatisticsDTO;
import com.xhwl.logistics.util.AboutDateUtil;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ToQueryVehiclePayRecordStatisticsBO {

    public QueryVehiclePayRecordStatisticsBO toQueryVehiclePayRecordStatisticsBO(VehiclePayRecordStatisticsDTO vehiclePayRecordStatisticsDTO) {
        QueryVehiclePayRecordStatisticsBO queryVehiclePayRecordStatisticsBO = new QueryVehiclePayRecordStatisticsBO();

        Integer projectId = vehiclePayRecordStatisticsDTO.getProjectId();
        String parkingLotName = vehiclePayRecordStatisticsDTO.getParkingLotName();

        Date date;
        Date time = vehiclePayRecordStatisticsDTO.getTime();
        if (null == time) {
            date = new Date();
        } else {
            date = time;
        }

        Integer type = vehiclePayRecordStatisticsDTO.getType();
        Date handleDate;
        if (type.equals(1)) {
            handleDate = AboutDateUtil.theNextDayOfDate(date, VehicleConstant.vehicleTimeZone);
            queryVehiclePayRecordStatisticsBO.setStartTime(AboutDateUtil.dateMinusDay(handleDate, 7));
            queryVehiclePayRecordStatisticsBO.setEndTime(handleDate);
            queryVehiclePayRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        } else if (type.equals(2)) {
            handleDate = AboutDateUtil.theNextDayOfDate(date, VehicleConstant.vehicleTimeZone);
            queryVehiclePayRecordStatisticsBO.setStartTime(AboutDateUtil.dateMinusDay(handleDate, 30));
            queryVehiclePayRecordStatisticsBO.setEndTime(handleDate);
            queryVehiclePayRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        } else if (type.equals(3)) {
            handleDate = AboutDateUtil.theNextMonthOfDate(date, VehicleConstant.vehicleTimeZone);
            queryVehiclePayRecordStatisticsBO.setStartTime(AboutDateUtil.dateMinusYear(handleDate, 1));
            queryVehiclePayRecordStatisticsBO.setEndTime(handleDate);
            queryVehiclePayRecordStatisticsBO.setDateFormatString("%Y-%m");
        } else if (type.equals(4)) {
            queryVehiclePayRecordStatisticsBO.setStartTime(AboutDateUtil.theFirstDayOfWeek(date, VehicleConstant.vehicleTimeZone));

            Date theSameDay = AboutDateUtil.theSameDayOfDateAnother(date, VehicleConstant.vehicleTimeZone);
            queryVehiclePayRecordStatisticsBO.setEndTime(AboutDateUtil.datePlusDay(theSameDay, 1));
            queryVehiclePayRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        } else if (type.equals(5)) {
            queryVehiclePayRecordStatisticsBO.setStartTime(AboutDateUtil.theSameMonthOfDate(date, VehicleConstant.vehicleTimeZone));

            Date theSameDay = AboutDateUtil.theSameDayOfDateAnother(date, VehicleConstant.vehicleTimeZone);
            queryVehiclePayRecordStatisticsBO.setEndTime(AboutDateUtil.datePlusDay(theSameDay, 1));
            queryVehiclePayRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        } else if (type.equals(6) || type.equals(7)) {
            handleDate = new Date();
            queryVehiclePayRecordStatisticsBO.setStartTime(DateUtil.beginOfYear(handleDate));
            queryVehiclePayRecordStatisticsBO.setEndTime(DateUtil.endOfYear(handleDate));
            queryVehiclePayRecordStatisticsBO.setDateFormatString("%Y-%m");
        } else {
            throw new BusinessException("查询缴费统计，输入的参数type值非法");
        }

        queryVehiclePayRecordStatisticsBO.setType(type);
        queryVehiclePayRecordStatisticsBO.setProjectId(projectId);
        queryVehiclePayRecordStatisticsBO.setParkingLotName(parkingLotName);

        return queryVehiclePayRecordStatisticsBO;
    }

}
